package com.ggh.jinjilive.ui.mall.goods;

import android.content.Intent;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.mall.MallCommunityLIst;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.mobile.mall.MallPointsClass;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.huantansheng.easyphotos.models.album.entity.Photo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class GoodsViewModel extends BaseViewModel {
    //分类
    public MutableLiveData<List<MallPointsClass>> listMutableLiveData = new MutableLiveData<>();
    //分类ID
    public ObservableField<Integer> tabId = new ObservableField<>(0);
    //搜索内容
    public ObservableField<String> keywords = new ObservableField<>("");
    //商品
    public MutableLiveData<List<MallGoods>> mMallGoodsList = new MutableLiveData<>();
    //社区
    public MutableLiveData<List<MallCommunityLIst>> mMallCommunityLIst = new MutableLiveData<>();

    public GoodsViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 购物车
     */
    public void tv_mall_shopping(){
        ARouter.getInstance().build(ARouterUtils.ShopingCarActivity)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }

    /**
     * 收藏
     */
    public void tv_mall_collection(){
        ARouter.getInstance().build(ARouterUtils.CollectionActivity)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }
    /**
     * 关注店铺
     */
    public void tv_mall_Focus_shop(){
        ARouter.getInstance().build(ARouterUtils.AttentionShopActivity)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }

    /**
     * 收货地址
     */
    public void tv_mall_Thegoods(){
        ARouter.getInstance().build(ARouterUtils.TheGoodsActivity)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }

    /**
     * 我的订单
     */
    public void ll_mall_goods(){
        ARouter.getInstance().build(ARouterUtils.OrderActivity)
                .withInt("index",0)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }
    /**
     * 待支付
     */
    public void ll_mall_stay_payment(){
        ARouter.getInstance().build(ARouterUtils.OrderActivity)
                .withInt("index",1)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }
    /**
     * 待发货
     */
    public void ll_mall_stay_hair_cargo(){
        ARouter.getInstance().build(ARouterUtils.OrderActivity)
                .withInt("index",2)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }
    /**
     * 待收货
     */
    public void ll_mall_stay_closed_cargo(){
        ARouter.getInstance().build(ARouterUtils.OrderActivity)
                .withInt("index",3)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }
    /**
     * 售后服务
     */
    public void ll_mall_after_sales_service(){
        ARouter.getInstance().build(ARouterUtils.OrderActivity)
                .withInt("index",4)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    }

    /**
     * 分类
     */
    public void productType(){
        Map<String, Object> map = new HashMap<>();
        addSubscribe(apiCallInterface.productType(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MallPointsClass>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MallPointsClass> mallPointsClassList = (List<MallPointsClass>) resultEntity.getData();
                listMutableLiveData.setValue(mallPointsClassList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }

    /**
     * 商品
     */
    public void productList(){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("type",tabId.get());
        map.put("keywords", keywords.get());
        map.put("page",page +"");
        map.put("limit",pageSize);
        addSubscribe(apiCallInterface.productList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MallGoods>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MallGoods> mallGoodsList = (List<MallGoods>) resultEntity.getData();
                mMallGoodsList.setValue(mallGoodsList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }

    /**
     * 社区
     */
    public void communityList(){
        Map<String, Object> map = new HashMap<>();
        map.put("lat",MerchantSession.getInstance().getUser().getLat());
        map.put("lon",MerchantSession.getInstance().getUser().getLng());
        map.put("keywords", keywords.get());
        addSubscribe(apiCallInterface.communityList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MallCommunityLIst>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MallCommunityLIst> mallCommunityLIstList = (List<MallCommunityLIst>) resultEntity.getData();
                mMallCommunityLIst.setValue(mallCommunityLIstList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }


    /**
     * 上架商品
     * @param map
     */
    public void editProduct(Map<String, Object> map){
        addSubscribe(apiCallInterface.editProduct(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {

            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }


    /**
     * 商品封面
     */
    public void uploadimages(String path, AddOnClickListener1 addOnClickListener1){
        File file = new File(path);
        RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), fileRequestBody);
        addSubscribe(apiCallInterface.uploadimages(body).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<CommonMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                CommonMobile commonMobile = (CommonMobile) resultEntity.getData();
                addOnClickListener1.onSkipClicked(commonMobile.getUrl());
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                ToastUtils.make().show(netMsg);
            }

        }));
    }

    /**
     * 循环上传图片
     * @param photoList
     */
    public void uploadimagesList(ArrayList<Photo> photoList){
        List<MultipartBody.Part> parts = new ArrayList<>();
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (int i = 0; i < photoList.size(); i++) {
            //多图片上传用循环
            File file = new File(photoList.get(i).path);
            RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("files", file.getName(), fileRequestBody);
            parts.add(builder.build().part(i));
        }
        addSubscribe(apiCallInterface.uploadimages(parts).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {

            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                ToastUtils.make().show(netMsg);
            }

        }));


    }


}
